<?php

namespace My\Controller;

use Utuo\BackgroundController;

class HouseSceneController extends BackgroundController {
	// 户型列表
	private $houseTypeLists = array();
	// 户型ID
	private $houseTypeId;
	
	// 初始化
	public function _initialize() {
		$this->setControllerTitle('场景管理');
		$this->setActionTitle(array(
				'index' => '场景列表',
				'modify' => '修改场景',
				'upload' => '上传照片',
				'recycle' => '回收站' 
		));
		$this->houseTypeLists = D('HouseType')->where('cid=%d and is_deleted=0', $this->aid)->getField('id,name');
		$this->houseTypeId = I('type_id', false, 'intval');
		$this->setActionTitle('add', '添加场景', 'type_id=' . $this->houseTypeId);
		$this->setActionNav('index,add,recycle');
		$this->setLinkNavTitle('户型导航');
		foreach ($this->houseTypeLists as $k => $v) {
			$this->setLinkNav($v, U('HouseScene/index', "type_id=$k"));
		}
	}
	
	// 列表
	public function indexAction() {
		$this->getHouseSceneLists(0);
	}
	
	// 回收站
	public function recycleAction() {
		$this->getHouseSceneLists(1);
	}
	
	// 获取列表
	private function getHouseSceneLists($is_deleted) {
		$condition = array(
				'a.cid' => $this->aid,
				'a.is_deleted' => $is_deleted 
		);
		$this->houseTypeId = I('type_id', false, 'intval');
		$this->houseTypeId && $condition['type_id'] = $this->houseTypeId;
		$dHouseScene = D('HouseScene a');
		$dHouseScene->join("__HOUSE_TYPE__ b on a.type_id = b.id",'LEFT');
		$houseSceneLists = $dHouseScene->where($condition)->field('a.*, b.name type_name')->select();
		foreach ($houseSceneLists as &$v) {
			$v['statusName'] = $dHouseScene->status[$v['status']];
		}
		$this->assign('houseSceneLists', $houseSceneLists);
		$this->display();
	}
	
	// 添加场景
	public function addAction() {
		C("TOKEN_ON", true);
		$dHouseScene = D('HouseScene');
		$houseScene = array();
		$houseScene['typeOptions'] = $this->getOption($this->houseTypeLists, $this->houseTypeId, '==请选择户型==');
		$houseScene['status'] = $this->getRadio('status', $dHouseScene->status, 1);
		$this->assign('houseScene', $houseScene);
		$this->display('save');
	}
	
	// 修改场景
	public function modifyAction() {
		$dHouseScene = D('HouseScene');
		$houseScene = $dHouseScene->find($this->getId());
		$houseScene['typeOptions'] = $this->getOption($this->houseTypeLists, $houseScene['type_id'], '==请选择户型==');
		$houseScene['status'] = $this->getRadio('status', $dHouseScene->status, $houseScene['status']);
		$this->assign('houseScene', $houseScene);
		$this->display('save');
	}
	
	// 保存场景
	public function saveAction() {
		$dHouseScene = D('HouseScene');
		$dHouseScene->submit() === false && $this->ajaxError($dHouseScene->getError());
		$this->ajaxSuccess();
	}
	
	// 上传图片
	public function uploadAction() {
		$images = D('HouseScene')->where('id=%d',$this->getId())->getField('pic');
		$images = explode('|', $images);
		$images = json_encode($images);
		$this->assign('images',$images);
		$this->display();
	}
	
	// 设置状态
	public function setStatusPostAction() {
		$id = I("id", 0, "intval");
		$status = I("status", 0, "intval");
		$d = D("HouseScene");
		if ($d->where("id={$id}")->setField("status", 1 - $status)) {
			$this->ajaxSuccess();
		} else {
			$this->ajaxError($d->getError());
		}
	}
	
	// 移除内容
	public function removePostAction() {
		$this->ajaxRemove ( "HouseScene" );
	}
	
	// 恢复内容
	public function restorePostAction() {
		$this->ajaxRestore ( "HouseScene" );
	}
	
	// 彻底删除内容
	public function deletePostAction() {
		$this->ajaxDelete( "HouseScene" );
	}
	
	
}